package com.dao;


import com.domain.actCharge;
import com.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class actChargeDao {

    //创建jdbcTemplate对象
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    //增加活动的负责员工
    public Integer addActCharge(actCharge act){
        //添加语句
        String sql="insert into actCharge values(null,?,?,?)";
        int update = jdbcTemplate.update(sql, act.getEmployeeId(),act.getActivityId(),act.getDuty());

        return update;
    }

    //根据员工的id删除
    public Integer deleteActCharge(actCharge id){
        //删除语句
        String sql="delete from actCharge where employeeid=?";
        //进行执行
        int update = jdbcTemplate.update(sql, id);
        //返回结果
        return update;
    }

    //根据员工id 和 活动id  更新数据
    public Integer updateActCharge(actCharge act){
        String sql="update actPlan set duty=? where employeeId=? and activityId=?";
        int update = jdbcTemplate.update(sql,
                act.getDuty(),
                act.getEmployeeId(),
                act.getActivityId());
        return update;
    }

    //查询方法,查询所有
    public List<actCharge> findAllActCharge(){
        //SQl
        String sql="select * from actCharge";
        //new BeanPropertyRowMapper<>(Activity.class)进行查询
        List<actCharge> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(actCharge.class));

        return list;
    }


    //按照职责  所有
    public List<actCharge> findActCharge(String duty){

        //sql
        String sql="select * from actCharge where duty LIKE \"%"+duty+"%\" ";

        //进行查询
        List<actCharge> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(actCharge.class));

        if (list==null || list.size()==0){
            return null;
        }

        return list;
    }

    //按照员工id查询
    public List<actCharge> findActCharge(Integer id){
        //sql
        String sql="select * from actCharge where employeeId=?";
        //进行查询
        List<actCharge> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(actCharge.class),id);

        if (list==null || list.size()==0){
            return null;
        }
        return list;
    }
}
